package cn.xt.forum.util;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Date;
import java.util.Properties;
import java.util.Random;

@Component
public class JavaMailUtils {

    public static String myEmailAccount;

    @Value("${exmail.account}")
    public void setMyEmailAccount(String myEmailAccount) {
        this.myEmailAccount = myEmailAccount;
    }

    public static String myEmailPassword;

    @Value("${exmail.password}")
    public void setMyEmailPassword(String myEmailPassword) {
        this.myEmailPassword = myEmailPassword;
    }

    public static String myEmailSMTPHost;

    @Value("${exmail.smtp.host}")
    public void setMyEmailSMTPHost(String myEmailSMTPHost) {
        this.myEmailSMTPHost = myEmailSMTPHost;
    }

    // 收件人邮箱
    public static String receiveMailAccount;
    public static String siteName;

    @Value("${site.main.title}")
    public void setSiteName(String siteName) {
        this.siteName = siteName;
    }

    public static String userName = siteName + "用户";
    public static String code;

    public static String getUserName() {
        return userName;
    }

    public static void init() {
        JavaMailUtils.userName = userName;
    }

    public static void setUserName(String userName) {
        JavaMailUtils.userName = userName;
    }

    public static String getReceiveMailAccount() {
        return receiveMailAccount;
    }

    public static void setReceiveMailAccount(String receiveMailAccount) {
        JavaMailUtils.receiveMailAccount = receiveMailAccount;
    }

    public static void send() throws Exception {

        Properties props = new Properties();                    // 参数配置
        props.setProperty("mail.transport.protocol", "smtp");   // 使用的协议（JavaMail规范要求）
        props.setProperty("mail.smtp.host", myEmailSMTPHost);   // 发件人的邮箱的 SMTP 服务器地址
        props.setProperty("mail.smtp.auth", "true");            // 需要请求认证

        Session session = Session.getDefaultInstance(props);
        session.setDebug(true);
        MimeMessage message = createMimeMessage(session, myEmailAccount, receiveMailAccount);


        Transport transport = session.getTransport();

        transport.connect(myEmailAccount, myEmailPassword);

        transport.sendMessage(message, message.getAllRecipients());

        transport.close();
    }

    /**
     * 创建一封只包含文本的简单邮件
     *
     * @param session     和服务器交互的会话
     * @param sendMail    发件人邮箱
     * @param receiveMail 收件人邮箱
     * @return
     * @throws Exception
     */
    public static MimeMessage createMimeMessage(Session session, String sendMail, String receiveMail) throws Exception {

        MimeMessage message = new MimeMessage(session);

        message.setFrom(new InternetAddress(sendMail, siteName + "官方邮件平台", "UTF-8"));


        message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(receiveMail, userName, "UTF-8"));


        message.setSubject("欢迎使用" + siteName + "，离完成就差一步了", "UTF-8");

        code = String.valueOf(new Random().nextInt(899999) + 100000);
        message.setContent("亲爱的用户：您好！感谢您使用" + siteName + "服务。您正在进行邮箱验证，请在验证码输入框中输入此次验证码："
                + "<font face=\"宋体, arial, sans-serif\" size=\"5\" color=\"#ff0000\">" + code + "</font>"
                + "以完成验证。如非本人操作，请忽略此邮件，由此给您带来的不便请谅解！", "text/html;charset=UTF-8");


        message.setSentDate(new Date());

        message.saveChanges();

        return message;
    }
}
